/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;
using System.Collections;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Line chart. There are seven type of line chart, you can specialize it by EQLineChartType enum.
	/// </summary>
	/// <example>
	/// <br>EQLineChart linechart = new EQLineChart(EQLineChartType.Line);</br>
	/// <br>linechart.ChartTitle = "Line Chart Analysis";</br>
	/// <br>linechart.AddDataColumnIndex(3, "Price"); //Add a data source column titled "Price" binding to the forth column in the datatable of the EQTable object</br>
	/// <br>linechart.AddDataColumnIndex(4, "Stock");</br>
	/// <br>linechart.LabelColumnIndex = 1;</br>
	/// <br>linechart.DataLabelsType = EQDataLabelsType.ShowPercent;</br>
	/// <br>linechart.Left = 6;</br>
	/// <br>linechart.Top = 4;</br>
	/// <br>objEQTable.Chart = linechart;</br>
	/// <br>We assume that objEQTable has been added into the EQWorksheet object which will output to a excel document.</br>
	/// </example>
	public class EQLineChart: EQSeriesChart
	{
		/// <summary>
		/// Set/Get line chart type
		/// </summary>
		public new EQLineChartType ChartType
		{
			get
			{
				return EQEnumKit.ToEQLineChartType(base.ChartType);
			}
			set
			{
				base.ChartType = EQEnumKit.ToXlChartType(value);
			}
		}

		/// <summary>
		/// Initialize object by specializing line chart type
		/// </summary>
		/// <param name="chartType"></param>
		public EQLineChart(EQLineChartType chartType)
		{
			this.ChartType = chartType;
		}
		

		/// <summary>
		/// Output line chart on the excel document
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			this.DoBeforeOperationEvent(objWorksheet);

			//*****Set source of the chart and get Excel.Chart COM interface*****
			Excel.Chart objChart = this.InitializeChart(objWorksheet);

			int seriesCount = ((Excel.SeriesCollection)objChart.SeriesCollection(this.Missing)).Count;
			while(seriesCount>0)	//Delete all series
			{
				Excel.Series objSeries = (Excel.Series)objChart.SeriesCollection(1);
				objSeries.Delete();
				seriesCount --;
			}

			Excel.SeriesCollection objSeriesCollection = objChart.SeriesCollection(this.Missing) as Excel.SeriesCollection;
			for(int i=0; i<this.DataColumnIndexs.Count; i++)	//Generated several series
				objSeriesCollection.NewSeries();

			//Selection of XValues
			int intLabelColumnIndex = this.ChartSource.TableLeft + this.LabelColumnIndex;
			int intTableBottom = this.ChartSource.ItemTop + this.ChartSource.Table.Rows.Count - 1;	//Get the last row index of table
			Excel.Range xValues = objWorksheet.get_Range(objWorksheet.Cells[this.ChartSource.ItemTop, intLabelColumnIndex], objWorksheet.Cells[intTableBottom, intLabelColumnIndex]) as Excel.Range;

			int index = 0;
			IDictionaryEnumerator objIDictionaryEnumerator = this.DataColumnIndexs.GetEnumerator();
			while(objIDictionaryEnumerator.MoveNext())	//Set every series one by one
			{
				Excel.Series objSeries = (Excel.Series)objChart.SeriesCollection(index+1);
				objSeries.Name = objIDictionaryEnumerator.Value as string;

				int intDataColumnIndex = this.ChartSource.TableLeft + (int)objIDictionaryEnumerator.Key;	//Mapping datatable of the object to column index on excel document
				Excel.Range objRange = objWorksheet.get_Range(objWorksheet.Cells[this.ChartSource.ItemTop, intDataColumnIndex], objWorksheet.Cells[intTableBottom, intDataColumnIndex]) as Excel.Range;
				objSeries.Values = objRange;
				objSeries.XValues = xValues;
				index ++;
			}

			base.Output(objWorksheet);

			this.DoAfterOperationEvent(objWorksheet);
		}

	}
}
